package com.solar.etl.transform;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.dom4j.Element;

import com.solar.etl.config.mapping.Field;
import com.solar.etl.spi.Itransform;

public class BizDateTransform implements Itransform {

	@Override
	public String transform(Field field, Element item) {
		field.destValue=bizDate(field.destValue);
		return null;
	}
	
	public String bizDate(String time){
		if(time==null||"".equals(time)){
			return "";
		}
		String myday="";
		String bday="";
		long x=0;
		try {
		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		SimpleDateFormat fomtday=new SimpleDateFormat("yyyy-MM-dd");  
	     Date date4;
		 date4 = fomtday.parse(time);
		 myday=fomtday.format(date4);
	     Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date4);
		 calendar.add(Calendar.DAY_OF_MONTH, -1);
		 calendar.getTime();
		 bday=fomtday.format(calendar.getTime());
	     calendar.add(Calendar.DAY_OF_MONTH, 2);
		 calendar.getTime();
	     String aday=fomtday.format(calendar.getTime());

	     Date date1=formatter.parse(bday+" 06:00:00");
	     Date date2=formatter.parse(time);
	     Date date3=formatter.parse(aday+" 06:00:00");
	     long l=date2.getTime()-date1.getTime();
	     x=date3.getTime()-date2.getTime();
		} catch (ParseException e) {
			return time;
		}
	     if(x>86400000){
	    	 return bday;
	     }
	     return myday;
	}

}
